import {Injectable} from '@angular/core';
import {SysAttachmentSearchForm} from '../entity/sys-attachment-search-form';
import {SysAttachment} from '../entity/sys-attachment';
import {BaseService} from '@sb/base';
import {Observable} from 'rxjs/index';
import {catchError} from 'rxjs/operators';
import {baseHandleError} from '@sb/base/core/service/base-http-service';
import {ReturnForm} from '@sb/base/core/entity';
import {BaseAttachmentService} from './base-attachment.service';

/**
 * 机构服务
 */
@Injectable({
    providedIn: 'root'
})
export class SysAttachmentService extends BaseService<SysAttachment, SysAttachmentSearchForm> implements BaseAttachmentService {

    basePath = 'sys/attachment';

    /**
     * 根据多个附件id查询
     * @param ids 主键集
     */
    getAttachments(ids: string[]): Observable<ReturnForm<SysAttachment[]>> {
        return this._http.post<ReturnForm<SysAttachment[]>>(`${this.url}/gets`, {['ids']: ids}).pipe(
            catchError(baseHandleError<ReturnForm<SysAttachment[]>>('gets'))
        );
    }

    uploadAttachment(file: FormData): Observable<ReturnForm<SysAttachment>> {
        return this._http.post<ReturnForm<SysAttachment>>(`${this.url}/upload`, file).pipe(
            catchError(baseHandleError<ReturnForm<SysAttachment[]>>('upload'))
        );
    }

    getReadAttachmentUrl(id): string {
        return `${this.url}/read/${id}`;
    }

    getReadAttachmentThumbUrl(id): string {
        return `${this.url}/read/thumb/${id}`;
    }
}
